$(function () {
    let userId = sessionStorage.getItem("userId");
    // console.log(userId, "-----qqq")
    findById(userId);
})

layui.use(['form', 'layer', 'jquery', 'laydate'],
    function () {
        $ = layui.jquery;
        var form = layui.form,
            layer = layui.layer
            , laydate = layui.laydate;
        //日期
        laydate.render({
            elem: '#birthday',
            trigger: 'click'
        });
        //自定义验证规则
        form.verify({
            nikename: function(value) {
                if (value.length < 5) {
                    return '昵称至少得5个字符啊';
                }
            },
            pass: [/(.+){6,12}$/, '密码必须6到12位'],
            repass: function(value) {
                if ($('#L_pass').val() != $('#L_repass').val()) {
                    return '两次密码不一致';
                }
            }
        });
        //表单取值
        layui.$('#LAY-component-form-getval').on('click', function () {
            var data = form.val('example');
            alert(JSON.stringify(data));
            console.log(data,"111111")
        });
        //监听提交
        form.on('submit(edit)',
            function (data) {
                data = data.field;
                let arr_box = [];
                $('input[type=checkbox][name=hobby]:checked').each(function () {
                    arr_box.push($(this).attr("title"));
                });
                //数组
                // console.log(arr_box);
                //js 将数组转换位字符串
                // console.log(data);
                data.hobby = arr_box.toString();
                // let aa=form.val('userAdd');
                //  console.log(JSON.stringify(aa));
                //发异步，把数据提交给php
                console.log(data);
                $.ajax({
                    url: '/back/user/edit',
                    data: data,
                    type: 'post',
                    dataType: 'json',
                    success: function (data) {
                        if (data.msg == 'success') {
                            layer.alert("编辑成功", {
                                    icon: 6
                                },
                                function () {
                                    //关闭当前frame
                                    xadmin.close();
                                    // 可以对父窗口进行刷新
                                    xadmin.father_reload();
                                });
                        } else {
                            layer.msg("编辑用户失败");
                        }
                    }
                });
                return false;
            });

    });

function findById(id) {
    $.ajax({
        url: '/back/user/findById',
        data: {id: id},
        dataType: "json",
        type: 'get',
        success: function (data) {
            console.log(data,"kankan");
            setUserData(data.data);
        }
    })
}

//赋值
function setUserData(data) {
    // console.log(data,"赋值");
    layui.use(['form'],
        function () {
            let form = layui.form;
            $("#id").val(data.id);
            $("#username").val(data.username);
            $("#phone").val(data.phone);
            $("#sex").val(data.sex);
            $("#email").val(data.email);
            $("#address").val(data.address);
            $("input[name='enable'][value='" + data.enable + "']").attr("checked", true);
            let hobby = data.hobby;
            let hobbys = hobby.split(",");
            $('input[type=checkbox][name=hobby]').each(function () {
                // arr_box.push($(this).attr("title"));
                for (let i = 0; i < hobbys.length; i++) {
                    if (hobbys[i] == $(this).attr("title")) {
                        $("input[name='hobby'][title='" + hobbys[i] + "']").attr("checked", true);
                    }
                }
            });
            $("#birthday").val(data.birthday);
            form.render();//重新渲染
        });

}